﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Linq.Expressions;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using SalesSys.Comm.DAL;
using SalesSys.Comm.Utility;
using SalesSys.DAL.Base;

namespace SalesSys.DAL.Models
{
    /*
     * 系统人员及权限相关内容实体
     */
    public class EntityTabConfig
    {
        public const string SysTabPrefix = "Sys_";
        public const string SalesTabPrefix = "Sal_";
        public const string PurchaseTabPrefix = "Pur_";
        public const string StockTabPrefix = "Sto_";

    }

    public abstract class SysMapper<T, TID> : BaseMap<T, TID> where T : DbEntityBase<TID>
    {

        public override string TablePrefix
        {
            get { return EntityTabConfig.SysTabPrefix; }
        }
    }
    /// <summary>
    /// 权限值
    /// </summary>
    public class RolesConst
    {
        /// <summary>
        /// 管理员，权限管理
        /// </summary>
        public const string Admin = "A";
        /// <summary>
        /// 客户专员
        /// </summary>
        public const string ClientManager = "1";
        /// <summary>
        /// 运营
        /// </summary>
        public const string OperationManagement = "2";
        /// <summary>
        /// 审批人
        /// </summary>
        public const string Approver = "3";
        /// <summary>
        /// 财务人员
        /// </summary>
        public const string FinancialStaff = "4";
        /// <summary>
        /// 客服专员
        /// </summary>
        public const string Service = "5";
        /// <summary>
        /// 驻厂
        /// </summary>
        public const string FieldMan = "6";
        /// <summary>
        /// 采购员
        /// </summary>
        public const string Buyer = "7";
        static List<Tuple<string, string>> itemList;

        public static List<Tuple<string, string>> GetList()
        {
            if (itemList == null)
            {
                var list = new List<Tuple<string, string>>();
                list.Add(Tuple.Create(Admin, "管理员"));
                list.Add(Tuple.Create(ClientManager, "客户专员"));
                list.Add(Tuple.Create(OperationManagement, "运营"));
                list.Add(Tuple.Create(Approver, "审批"));
                list.Add(Tuple.Create(FinancialStaff, "财务"));
                list.Add(Tuple.Create(Service, "客服"));
                list.Add(Tuple.Create(FieldMan, "驻厂"));
                list.Add(Tuple.Create(Buyer, "采购"));
                itemList = list;
            }

            return itemList;
        }

    }



    public class Employee : DbEntityBase<Guid>, SalesSys.DAL.Models.IEmployee
    {
        public static class OfficesConst
        {
            /// <summary>
            /// 负责人
            /// </summary>
            public const string Director = "负责人";
            /// <summary>
            /// 采购
            /// </summary>
            public const string Buyer = "采购";

        }
        public string FullName()
        {
            return Department.Name + "/" + Name;
        }
        //[StringLength(15,MinimumLength=6,ErrorMessage="登录名必须6~15位")]
        // [RegularExpression(RegexConst.LoginName,ErrorMessage= "用户名必须以字母开头，后跟字母或下划线")]

        /// <summary>
        /// 登录名
        /// </summary>
        public string LoginName { get; set; }

        /// <summary>
        /// 登录密码
        /// </summary>
        [XmlIgnore]
        [StringLength(15, MinimumLength = 6, ErrorMessage = "密码必须6~15位")]
        public string LoginPwd { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateTime { get; set; }
        /// <summary>
        /// 创建人
        /// </summary>
        public virtual Employee Creater { get; set; }
        /// <summary>
        /// 是否确认
        /// </summary>
        public bool IsConfirm { get; set; }
        /// <summary>
        /// 权限角色，简单约定，权限使用“|”分割
        /// </summary>
        public string Roles { get; set; }
        public string[] GetRoles()
        {
            if (string.IsNullOrWhiteSpace(Roles))
            {
                return new string[] { };
            }
            else
            {
                return Roles.Split('|');
            }
        }
        /// <summary>
        /// 是否拥有某个角色
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        public bool IsRole(string role)
        {
            return Roles != null && ((!string.IsNullOrWhiteSpace(role)) && Roles.IndexOf(role) >= 0);
        }

        public string SetRoles(params string[] roleList)
        {
            var rolesStr = string.Join("|", roleList);
            Roles = rolesStr;
            return Roles;
        }

        /// <summary>
        /// 姓名
        /// </summary>
        [Required(ErrorMessage = "名称不能为空")]
        [StringLength(15, MinimumLength = 2, ErrorMessage = "长度必须2~15位")]
        public string Name { get; set; }

        /// <summary>
        /// 身份证
        /// </summary>
        [RegularExpression(RegexConst.IdCard, ErrorMessage = "身份证格式不正确")]
        public string IDCard { get; set; }

        /// <summary>
        /// 生日
        /// </summary>
        public DateTime? Birthday { get; set; }

        /// <summary>
        /// 手机号码
        /// </summary>
        [RegularExpression(RegexConst.CellPhone, ErrorMessage = "手机号码格式不正确")]
        public string Phone { get; set; }

        /// <summary>
        /// 是否离职
        /// </summary>
        public bool IsLeave { get; set; }

        /// <summary>
        /// 是否锁定
        /// </summary>
        public bool IsLock { get; set; }

        /// <summary>
        /// 所在部门
        /// </summary>
        public virtual Department Department { get; set; }

        /// <summary>
        /// 职务,简单管理,参见：OfficesConst
        /// </summary>
        public string Offices { get; set; }

        /// <summary>
        /// Token，唯一标识
        /// </summary>
        public string Token { get; set; }

        public class Mapper : SysMapper<Employee, Guid>
        {
            public Mapper()
            {
                Property(p => p.Name).IsRequired().HasMaxLength(50);
            }

        }
    }

    /// <summary>
    /// 部门
    /// </summary>
    public class Department : TreeEntity<Guid, Department>
    {
        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 负责人编号
        /// </summary>
        public Guid ManagerId { get; set; }

        public class Mapper : BaseTreeMap<Department, Guid>
        {
            public Mapper()
            {
                Property(p => p.Name).HasMaxLength(50);
            }

            public override string TablePrefix
            {
                get { return EntityTabConfig.SysTabPrefix; }
            }
        }
    }

    /// <summary>
    /// 所在地
    /// </summary>
    public class District : TreeEntity<Guid, District>
    {
        public override string ToString()
        {
            return FillName;
        }

        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 全称：河南省洛阳市
        /// </summary>
        public string FillName { get; set; }
        /// <summary>
        /// 级别，省1，市2
        /// </summary>
        public int Lv { get; set; }
        public class Mapper : BaseTreeMap<District, Guid>
        {
            public Mapper()
            {
                Property(p => p.Name).HasMaxLength(50);
                Property(p => p.Lv);
            }

            public override string TablePrefix
            {
                get { return EntityTabConfig.SysTabPrefix; }
            }
        }
    }

    /// <summary>
    /// 系统参数
    /// </summary>
    public class SysConfig : DbEntityBase<Guid>
    {
        public string Group { get; set; }
        public string Key { get; set; }
        public string Value { get; set; }
        public string Value2 { get; set; }
        public string Value3 { get; set; }
        public string Remarks { get; set; }
        public class Mapper : SysMapper<SysConfig, Guid>
        {

        }
    }
}
